﻿using System;
using System.Collections.Generic;
using FlowCanvas;
using FlowCanvas.Nodes;
using LogSystem;
using NodeCanvas.Framework;
using ParadoxNotion;
using ParadoxNotion.Design;
using UnityEngine;
using Yoozoo.Gameplay.Liberty.AI;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class StreetEventOnKillEvent : EventNode
    {
        public static Action<int, int> OnKill;
        
        private FlowOutput onKillEvent;
        private int uniqueId;
        private Transform transform;
        private int killerUniqueId;
        
        protected override void RegisterPorts()
        {
            onKillEvent = AddFlowOutput("onKill");
            AddValueOutput<int>("uniqueId", () => { return uniqueId; });
            AddValueOutput<Vector3>("position", () =>
            {
                if (transform)
                {
                    return transform.position;
                }
                return Vector3.zero;
            });
            AddValueOutput<int>("killerUniqueId", () => { return killerUniqueId; });
        }

        public override void OnGraphStarted()
        {
            OnKill += OnKillFunc;
        }

        public override void OnGraphStoped()
        {
            OnKill -= OnKillFunc;
        }

        private void OnKillFunc(int id, int killerId)
        {
            uniqueId = id;
            killerUniqueId = killerId;
            AIEntity aiEntity = LibertyAIUtils.GetNpc(id);
            if (aiEntity)
            {
                if (aiEntity.battleAgent)
                {
                    transform = aiEntity.battleAgent.transform;
                }
                else
                {
                    transform = aiEntity.viewer.transform;
                }
            }
            onKillEvent.Call(new Flow());
        }
    }
}